// lib/widgets/post_list_item_widget.dart
import 'package:flutter/material.dart';

import '../../../models/post.dart';


class PostListItemWidget extends StatelessWidget {
  final Post post; // **类型改为 Post 模型**
  final VoidCallback onTap;

  const PostListItemWidget({super.key, required this.post, required this.onTap});

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      elevation: 2,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10),
      ),
      child: InkWell(
        onTap: onTap,
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // **使用 post.title**
              Text(
                post.title,
                style: const TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 18,
                ),
              ),
              const SizedBox(height: 8),
              // **使用 post.content**
              Text(
                post.content, // 假设内容只显示部分
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
                style: const TextStyle(fontSize: 14, color: Colors.grey),
              ),
              const SizedBox(height: 12),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      const Icon(Icons.thumb_up, size: 16, color: Colors.grey),
                      const SizedBox(width: 4),
                      // **使用 post.stats?.likeCount 或 post.voteNum**
                      // 根据你获取列表的接口返回的是 stats 还是直接的 vote_num 来选择
                      // 假设 getPosts 返回的数据结构里，点赞数在 stats.likeCount
                      Text('${post.stats?.likeCount }'), // 优先使用 stats.likeCount，否则使用 voteNum
                    ],
                  ),
                  Row(
                    children: [
                      const Icon(Icons.comment, size: 16, color: Colors.grey),
                      const SizedBox(width: 4),
                      // **使用 post.stats?.commentCount**
                      Text('${post.stats?.commentCount ?? 0}'),
                    ],
                  ),
                  // 显示作者名字 (可选)
                  Row(
                    children: [
                      const Icon(Icons.person, size: 16, color: Colors.grey),
                      const SizedBox(width: 4),
                      Text(post.authorName),
                    ],
                  ),
                  // 显示所属社区 (可选)
                  if (post.community?.communityName != null && post.community!.communityName!.isNotEmpty)
                    Row(
                      children: [
                        const Icon(Icons.group, size: 16, color: Colors.grey),
                        const SizedBox(width: 4),
                        Text(post.community!.communityName!),
                      ],
                    ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}